using System;
using System.Diagnostics;

namespace bOS.Microsoft.Windows
{
	/// <summary>
	/// Summary description for ProcessHandler.
	/// </summary>
	public class ProcessHandler
	{
		public ProcessHandler()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public static void Start(string sProcessFile, string sWorkingDirectory, string sArguments)
		{
			System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(sProcessFile);
			psi.UseShellExecute = true;
			psi.RedirectStandardOutput = false;
			psi.RedirectStandardInput = false;
			psi.RedirectStandardError = false;
			psi.WorkingDirectory=sWorkingDirectory;
			psi.CreateNoWindow = false;
			psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
			psi.Arguments = sArguments;
			

			System.Diagnostics.Process listFiles;
			listFiles = System.Diagnostics.Process.Start(psi);
			listFiles.EnableRaisingEvents = false;
		}

		public static void Start(string sProcessName, string sProcessFile, string sWorkingDirectory, bool bOneInstance)
		{
			if ( bOneInstance && ProcessHandler.IsRunning (sProcessName) )
				return;

			System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(sProcessFile);
			psi.UseShellExecute = true;
			psi.RedirectStandardOutput = false;
			psi.RedirectStandardInput = false;
			psi.RedirectStandardError = false;
			psi.WorkingDirectory=sWorkingDirectory;
			psi.CreateNoWindow = false;
			psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
			

			System.Diagnostics.Process listFiles;
			listFiles = System.Diagnostics.Process.Start(psi);
			listFiles.EnableRaisingEvents = false;
		}

		public static bool Kill(string sProcessName, bool bAll)
		{
			Process[] pProcess; 
			pProcess = Process.GetProcessesByName(sProcessName);

			int iSize = pProcess.GetUpperBound(0);
			if ( iSize < 0 ) 
			{
				return false;
			}

			if ( !bAll )
				iSize=0;
			
			for ( int i=0; i<iSize;i++)
			{
				pProcess[i].Kill();
			}

			return true;
		}

		public static bool IsRunning(string sProcessName)
		{
			Process[] pProcess; 
			pProcess = Process.GetProcessesByName(sProcessName);

			int iSize = pProcess.GetUpperBound(0);

			if ( iSize >= 0 ) 
				return true;

			return false;
		}
	}
}
